package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
)

type Article struct {
	filename string
	link     string
}

var links = []Article{
	Article{
		filename: "articles/适用于电商企业的AB测试：7个实操示例.html",
		link:     "https://www.shopify.com/zh/blog/ab-testing-examples",
	},
	Article{
		filename: "articles/什么是垂直市场？定义和指南 (2025).html",
		link:     "https://www.shopify.com/zh/blog/what-is-a-vertical-market",
	},
	Article{
		filename: "articles/什么是AB测试：案例分享.html",
		link:     "https://www.shopify.com/zh/blog/the-complete-guide-to-ab-testing",
	},
	Article{
		filename: "articles/怎样做播客？11步教程助你开播 怎样做播客？11步教程助你开播发声.html",
		link:     "https://www.shopify.com/zh/blog/34911301-how-to-start-a-podcast-the-ultimate-step-by-step-podcasting-guide",
	},
	Article{
		filename: "articles/什么是企业对消费者（B2C）？有哪些B2C案例？.html",
		link:     "https://www.shopify.com/zh/blog/what-is-business-to-consumer-b2c-definition-and-guide",
	},
	Article{
		filename: "articles/什么是存货单位（SKU）？ 定义和指南.html",
		link:     "https://www.shopify.com/zh/blog/what-is-a-stock-keeping-unit",
	},
	Article{
		filename: "articles/SEO营销是什么？如何进行SEO营销？.html",
		link:     "https://www.shopify.com/zh/blog/seo-marketing",
	},
	Article{
		filename: "articles/什么是国家代码顶级域名（ccTLD）？.html",
		link:     "https://www.shopify.com/zh/blog/cctld",
	},
}

func main() {
	fmt.Println("begin...")

	for _, v := range links {
		saveToFile(v.filename, v.link)
	}

	fmt.Println("finish...")
}

// 获取html内容
func getHtmlContent(link string) (string, error) {
	resp, err := http.Get(link)
	if err != nil {
		fmt.Println("[http.Get] ", err)
		return "", err
	}

	defer resp.Body.Close()

	b, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("[io.ReadAll] ", err)
		return "", err
	}

	return string(b), nil
}

// 保存到文件
func saveToFile(filename string, link string) {
	content, err := getHtmlContent(link)
	if err != nil {
		fmt.Println("[getHtmlContent] ", link, err)
		return
	}

	f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		fmt.Println("[os.OpenFile] ", link, err)
		return
	}

	defer f.Close()

	f.WriteString(content)
}
